package ua.pp.shell.wka;
import java.util.Date;
import java.util.List;
import java.util.Random;
import ua.pp.shell.wka.DBAdapter.Types.Vocabulary;
import ua.pp.shell.wka.DBAdapter.Types.Word;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class TeacherActivity extends Activity {
	private  DBAdapter dbAdapter = null;
	private Vocabulary vocabulary = null;
	private Word word = null;
	private List<Word> words = null;
	private int count_words = 20;
	private int c_variants = 6;
	private Random rand = null;
	private boolean reversed = false;
	public class Components {
		TextView activity_title_label = null;
		TextView word_label = null;
		ListView translation_list = null;
		Button edit_button = null;
		public Components() {
			activity_title_label = (TextView)findViewById(R.id.activity_title);
			word_label = (TextView)findViewById(R.id.word);
			translation_list = (ListView)findViewById(R.id.translation);
			edit_button = (Button)findViewById(R.id.edit);
			edit_button.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getApplicationContext(), AEWordActivity.class);
		        	intent.putExtra("id", word.id);
		        	startActivityForResult(intent, App.activities.AE_WORD.ordinal());
				}
			});
			translation_list.setOnItemClickListener(new  AdapterView.OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int pos, long row) {
					String answer = ((TextView)view.findViewById(android.R.id.text1)).getText().toString();
	            	Date date = new Date();
	        		long now = date.getTime();
	        		String to_equal = "";
	        		if (reversed) {
	        			to_equal = word.word;
	        		} else {
	        			to_equal = word.translation;
	        		}
	            	if (to_equal.equals(answer)) {
	        			//success
	            		word.c_success++;
	            		word.t_last_success = now;
	            		Toast.makeText(getApplicationContext(), R.string.right, 2000).show();
	        		} else {
	        			//error
	        			word.c_error++;
	        			word.t_last_error = now;
	        			if (reversed) {
	        				Toast.makeText(getApplicationContext(), word.word, 2000).show();
	        			} else {
	        				Toast.makeText(getApplicationContext(), word.translation, 2000).show();
	        			}
	        		}
	            	word.c_shown++;
	            	word.t_last_seen = now;
	            	dbAdapter.UpdateWord(word);
	            	GetWord();
	            	ViewWord();
				}
			});
		}
	}
	
	public Components c = null;
	
	 @Override
	 public boolean onCreateOptionsMenu(Menu menu) {
		 MenuInflater inflater = getMenuInflater();
	     inflater.inflate(R.menu.teach, menu);
	     return true;
	 }
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rand = new Random();
        dbAdapter = new DBAdapter(this);
        Intent intent = this.getIntent();
        Settings settings = new Settings(getApplicationContext());
        int id = intent.getIntExtra("id", settings.GetCurrentVocabulary());
        if (id < 0) {
        	finish();
        }
       	vocabulary = dbAdapter.GetVocabulary(id);
        setContentView(R.layout.teacher_activity);
        c = new Components();
        GetWord();
        ViewWord();
        c.activity_title_label.setText(getString(R.string.vocabulary)+": "+vocabulary.title);
    }
    
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
        	case R.id.reverse:
        		reversed = !reversed;
        		ViewWord();
        		break;
    	}
    	return true;
    }
        		
    
    public void GetWord() {
    	words = dbAdapter.GetWordsFromVocabularyByKnowledge(vocabulary.id, count_words, c_variants);
    	if (words.size() == 0) {
    		Intent intent = new Intent();
            intent.putExtra("message", getString(R.string.vocabulary_is_empty));
            setResult(RESULT_CANCELED, intent);
            finish();  
    		return;
    	}
    	word = words.get(rand.nextInt(words.size()));
    }
    
    public void ViewWord(){
    	c.word_label.setTag(word);
    	if (reversed) {
    		c.word_label.setText(word.translation);
    	} else {
    		c.word_label.setText(word.word);
    	}
    	String[] word_strings = new String[words.size()];
    	for(int i = 0; i < words.size(); i++) {
    		if (reversed) {
    			word_strings[i] = ((Word)words.get(i)).word;
    		} else {
    			word_strings[i] = ((Word)words.get(i)).translation;
    		}
    	}
    	c.translation_list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, word_strings));
   }
    
}
